package com.eascs.admin.item.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.item.entity.O2oPubDatadictionaryEntity;
import com.eascs.admin.item.entity.O2oPubDditemEntity;
import com.eascs.admin.item.service.IO2oPubDdItemService;
import com.eascs.admin.item.service.O2oPubDdItemService;
import com.eascs.admin.item.vo.O2oPubDatadictionaryForm;
import com.eascs.admin.item.vo.O2oPubDdItemForm;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

@RestController
@RequestMapping("/ritem")
public class O2oPubDdItemRestController extends BaseRestController {

	@Autowired
	O2oPubDdItemService ddItemService;
	@Reference
	IO2oPubDdItemService pubDdItemService;
	
	@RequestMapping("/findDd")
	public BaseRestResult<String> findDd() {
		List<Object[]> dataSouceList = pubDdItemService.findPubDd();
		ViewContext.put("vo", dataSouceList);
//		String name = "";
//		for (int i = 0; i < dataSouceList.size(); i++) {
//			Object[] objects = dataSouceList.get(i);
//			for (int j = 0; j < objects.length; j++) {
//				name += objects[j].toString()+":";
//			}
//			name += ",";
//		}
		return HtmlRestResult.info("哈哈");
	}

	@RequestMapping("/editDdHtml")
	public BaseRestResult<String> editDdHtml(String ddNo) {
		O2oPubDatadictionaryEntity datadictionaryEntity = ddItemService.findPubDatadictionaryById(ddNo);
		ViewContext.put("vo", datadictionaryEntity);
		return HtmlRestResult.popHtmlFragment("item/editDd");
	}

	@RequestMapping("/editHtml")
	public BaseRestResult<String> editHtml(String ddNo) {
		O2oPubDditemEntity dditemEntity = ddItemService.findPubDdItemById(ddNo);
		ViewContext.put("vo", dditemEntity);
		return HtmlRestResult.popHtmlFragment("item/edit");
	}

	@RequestMapping("/addHtml")
	public BaseRestResult<String> addHtml(String ddNo) {
		ViewContext.put("ddNo", ddNo);
		return HtmlRestResult.popHtmlFragment("item/add");
	}

	@RequestMapping("/addDdHtml")
	public BaseRestResult<String> addDdHtml() {
		return HtmlRestResult.popHtmlFragment("item/addDd");
	}

	@RequestMapping("/editDd")
	public BaseRestResult<String> editDd(O2oPubDatadictionaryForm form) {
		O2oPubDatadictionaryEntity sourceEntity = ddItemService.findPubDatadictionaryByCode(form.getDdCode());
		if (null != sourceEntity) {
			if (!sourceEntity.getDdNo().equals(form.getDdNo())) {
				return BaseRestResult.warn("数据字典CODE已被使用");
			}
		}
		O2oPubDatadictionaryEntity datadictionaryEntity = new O2oPubDatadictionaryEntity();
		datadictionaryEntity.setDdNo(form.getDdNo());
		datadictionaryEntity.setDdCode(form.getDdCode());
		datadictionaryEntity.setDdDesc(form.getDdDesc());
		datadictionaryEntity.setDdName(form.getDdName());
		datadictionaryEntity.setIsLock(StatusContexts.O2O_NO);
		int i = ddItemService.updatePubDatadictionaryById(datadictionaryEntity);
		if (i > 0) {
			return BaseRestResult.info("修改成功").andRefresh();
		} else {
			return BaseRestResult.warn("修改失败，请稍后重试");
		}
	}

	@RequestMapping("/edit")
	public BaseRestResult<String> edit(O2oPubDdItemForm ddItemForm) {
		O2oPubDditemEntity sourceEntity = ddItemService.findPubDdItemByCode(ddItemForm.getItemCode(), ddItemForm.getDdNo());
		if (null != sourceEntity){
			if (!sourceEntity.getItemNo().equals(ddItemForm.getItemNo())) {
				return BaseRestResult.warn("常量CODE已被使用");
			}
		}
		O2oPubDditemEntity dditemEntity = new O2oPubDditemEntity();
		dditemEntity.setItemNo(ddItemForm.getItemNo());
		dditemEntity.setItemCode(ddItemForm.getItemCode());
		dditemEntity.setItemName(ddItemForm.getItemName());
		dditemEntity.setItemDesc(ddItemForm.getItemDesc());
		dditemEntity.setDdNo(ddItemForm.getDdNo());
		dditemEntity.setItemOrder(ddItemForm.getItemOrder());
		dditemEntity.setIsLock(StatusContexts.O2O_NO);
		dditemEntity.setIsSync(StatusContexts.O2O_NO);
		int i = ddItemService.updatePubDdItemById(dditemEntity);
		if (i > 0) {
			return BaseRestResult.info("修改成功").andRefresh();
		} else {
			return BaseRestResult.warn("修改失败，请稍后重试");
		}
	}

	@RequestMapping("/add")
	public BaseRestResult<String> add(O2oPubDdItemForm ddItemForm) {
		O2oPubDditemEntity sourceEntity = ddItemService.findPubDdItemByCode(ddItemForm.getItemCode(), ddItemForm.getDdNo());
		if (null != sourceEntity){
			return BaseRestResult.warn("常量CODE已被使用");
		}
		O2oPubDditemEntity dditemEntity = new O2oPubDditemEntity();
		dditemEntity.setItemCode(ddItemForm.getItemCode());
		dditemEntity.setItemName(ddItemForm.getItemName());
		dditemEntity.setItemDesc(ddItemForm.getItemDesc());
		dditemEntity.setDdNo(ddItemForm.getDdNo());
		dditemEntity.setItemOrder(ddItemForm.getItemOrder());
		dditemEntity.setIsLock(StatusContexts.O2O_NO);
		dditemEntity.setIsSync(StatusContexts.O2O_NO);
		int i = ddItemService.savePubDdItem(dditemEntity);
		if (i > 0) {
			return BaseRestResult.info("添加成功").andRefresh();
		} else {
			return BaseRestResult.warn("添加失败，请稍后重试");
		}
	}

	@RequestMapping("/addDd")
	public BaseRestResult<String> addDd(O2oPubDatadictionaryForm form) {
		O2oPubDatadictionaryEntity sourceEntity = ddItemService.findPubDatadictionaryByCode(form.getDdCode());
		if (null != sourceEntity) {
			return BaseRestResult.warn("数据字典CODE已被使用");
		}
		O2oPubDatadictionaryEntity datadictionaryEntity = new O2oPubDatadictionaryEntity();
		datadictionaryEntity.setDdCode(form.getDdCode());
		datadictionaryEntity.setDdDesc(form.getDdDesc());
		datadictionaryEntity.setDdName(form.getDdName());
		datadictionaryEntity.setIsLock(StatusContexts.O2O_NO);
		int i = ddItemService.savePubDatadictionary(datadictionaryEntity);
		if (i > 0) {
			return BaseRestResult.info("添加成功").andRedirect("/item/index?itemCode=" + form.getDdCode());
		} else {
			return BaseRestResult.warn("添加失败，请稍后重试");
		}
	}

	@RequestMapping("/start_up")
	public BaseRestResult<String> start_up(String ddNo, String type) {
		String sql = "";
		if (O2oPubDdItemForm.DD_START.equals(type)) {
			sql = "update pub_datadictionary_o2o set is_lock='" + StatusContexts.O2O_NO + "' where dd_no=? ";
		} else if (O2oPubDdItemForm.DD_STOP.equals(type)) {
			sql = "update pub_datadictionary_o2o set is_lock='" + StatusContexts.O2O_YES + "' where dd_no=? ";
		} else if (O2oPubDdItemForm.DDO_START.equals(type)) {
			sql = "update pub_dditem_o2o set is_lock='" + StatusContexts.O2O_NO + "' where item_no=? ";
		} else if (O2oPubDdItemForm.DDO_STOP.equals(type)) {
			sql = "update pub_dditem_o2o set is_lock='" + StatusContexts.O2O_YES + "' where item_no=? ";
		} else {
			return BaseRestResult.warn("暂时无法修改，请稍后重试").andRefresh();
		}
		int i = ddItemService.startAndStop(sql, ddNo);
		if (i > 0) {
			return BaseRestResult.refresh();
		} else {
			return BaseRestResult.warn("修改失败，请稍后重试").andRefresh();
		}
	}
}
